#!/usr/bin/python
# -*- coding: cp1250 -*-
#import sys; sys.path.append(r'C:/xxx/yyy')

import glob, re, string, pepe.p_txt.bigtxt as bt
from pepe.glib import *
import xlrd
from pyExcelerator.Workbook import *
from pyExcelerator.Style import *

#xlrd
book = xlrd.open_workbook('Index.xls')
sh = book.sheet_by_index(0)

#pyExcelerator
style = XFStyle()
wb = Workbook()
ws = wb.add_sheet('Search_Summary')

i = range(sh.nrows)[-1]+1
r = 0
c = 0
ce = 0

res = {}
for n in range (0,i):
        col = sh.cell_value(rowx=n, colx=9)       
        col = string.split(str(col), ';')                  
        l = len(col)        
        for i in range(0,l):            
            cols = string.split(col[i], ',')
            if len(cols)== 2:
                k = string.strip(cols[0])                 
                v = int(string2number(cols[1]))   
                if res.has_key(k):
                   res[k][0] += v
                   res[k][1] += 1                
                if not res.has_key(k):
                   res[k] = [v, 1]
            else:
                ws.write(0, 5, 'Skipped cells:')
                x = str(string.join(col, ';'))                
                ws.write(ce, 6, x)
                ce += 1

ws.write(0, 0, 'Word')
ws.write(0, 1, 'All_Hits')
ws.write(0, 2, 'Docs_Hits')

for k in sorted(res.keys()):
    p =(k + '\n')   
    r += 1
    ws.write(r, c, p)

c = 1
r = 0
for k in sorted(res.keys()):
    p = res[k][0]  
    r += 1
    ws.write(r, c, p)

c = 2
r = 0
for k in sorted(res.keys()):
    p = res[k][1]   
    r += 1
    ws.write(r, c, p)
    ws.col(0).width = 6000
    ws.col(1).width = 3000
    ws.col(2).width = 3000
    ws.col(5).width = 3500
    ws.col(6).width = 5000
  
wb.save('Search_Summary.xls')

